package br.com.seosoft.simplecalcfinan.adapters;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import br.com.seosoft.simplecalcfinan.R;
import br.com.seosoft.simplecalcfinan.geral;
import br.com.seosoft.simplecalcfinan.R.id;
import br.com.seosoft.simplecalcfinan.R.layout;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListaAdapterContas extends BaseAdapter {

	private Context context;
	private List<geral> geralList;
	private SimpleDateFormat formatar;

	public ListaAdapterContas(Context context, List<geral> geralList) {
		this.context = context;
		this.geralList = geralList;
		this.formatar = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
	}

	@Override
	public int getCount() {
		return geralList.size();
	}

	@Override
	public Object getItem(int position) {
		return geralList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub

		return geralList.isEmpty();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// Recupera o estado da posi����o atual
		geral geral = geralList.get(position);

		// Cria uma inst��ncia do layout XML para os objetos correspondentes
		// na View
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.lista_contas, null);
		}

		// DESCRICAO GRUPO
		TextView textDescricao = (TextView) convertView
				.findViewById(R.id.CampoDescricaoConta);
		textDescricao.setText(geral.getDescConta());

		// SALDO CONTAS
		TextView textSaldoContas = (TextView) convertView
				.findViewById(R.id.CampoValorConta);
		textSaldoContas.setText(geral.DecimalCurrency(geral.getSaldoConta()));

		// DATA

		TextView textDataConta = (TextView) convertView
				.findViewById(R.id.CampoDataConta);

		if (geral.getData() != null) {
			textDataConta.setText(formatar.format(geral.getData()));
		} else {
			textDataConta.setText("data não definida");
		}
		Log.i(geral.LogCat, "Data: " + geral.getData());

		// MODIFICA CORES
		textSaldoContas.setTextColor(geral.mudarCor(geral.getSaldoConta()));

		return convertView;
	}

}
